<template>
    <div id="Group"  style="text-align:left">
        <el-checkbox
                :indeterminate="isIndeterminate"
                v-model="checkAll"
                @change="handleCheckAllChange"
        >全选</el-checkbox>
        <el-card style="height:400px">
            <el-scrollbar style="height:100%">
                <div  v-for="(group,index) in groupData" :key="index">
                    <el-divider></el-divider>
                    <span>{{group.Name}}</span>

                    <div v-if="selectType == true">
                        <el-checkbox-group v-model="multipleSelect" @change="handleCheckedChange">
                                <span v-for="entity in entityData" :key="entity.No">
                                    <el-checkbox v-if="entity[keyOfEn] == group[keyOfGroup]" :label="entity.No" >{{entity.Name}}</el-checkbox>
                                </span>
                        </el-checkbox-group>
                    </div>
                    <div v-else>
                            <span v-for="entity in entityData" :key="entity.No">
                                <el-radio
                                        v-if="entity[keyOfEn] == group[keyOfGroup]"
                                        v-model="radioSelect"
                                        :label="entity.No"
                                        @change="radioClick(entity)"
                                >{{entity.Name}}</el-radio>
                            </span>
                    </div>

                </div>
            </el-scrollbar>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "Group",
        props:{
            mapExt:{
                type:Object,
            },
            selectRow:{
                type:Array,
            }
        },
        data(){
            return{
                params:{},
                webUser:{},
                isIndeterminate: true,
                checkAll: false,
                selectType:true,//默认是多选
                colNum:3,//显示的列数
                multipleSelect: [],
                radioSelect:"",
                groupData:[],
                entityData:[],
                keyOfEn:"",
                keyOfGroup:""
            }
        },
        created(){
            this.params = this.$store.getters.getData;
            this.webUser=this.$store.getters.getWebUser;
            this.selectType=this.GetPara(this.mapExt.AtPara,"SelectType")=="0"?false:true;
            this.colNum = this.GetPara(this.mapExt.AtPara,"ShowCol");
            if(this.colNum==null || this.colNum==undefined ||this.colNum=="" ||this.colNum=="0")
                this.colNum=3;
            if(this.selectRow.length>0){
                this.radioSelect = this.selectRow[0].No;
                this.selectRow.forEach(item=>{
                    this.multipleSelect.push(item.No);
                });
            }

            this.InitData();
        },
        methods:{
            InitData(){
                debugger
                if (this.mapExt.DBType == 0) {
                    let en = new this.Entity("BP.Sys.MapExt",this.mapExt);
                    en.MyPK = this.mapExt.MyPK;
                    let data = en.DoMethodReturnString("GetDataTableByField", "Tag1","",null,this.params.WorkID); //分组.
                    if (data.indexOf("err@") != -1) {
                        this.$message.error(data);
                        console.log(data);
                        return;
                    }
                    this.groupData = JSON.parse(data);
                    data = en.DoMethodReturnString("GetDataTableByField", "Tag2", "", null, this.params.WorkID);//数据源.
                    if (data.indexOf("err@") != -1) {
                        this.$message.error(data);
                        console.log(data);
                        return;
                    }
                    this.entityData = JSON.parse(data);
                } else {
                    let srcOfGroup = this.mapExt.Tag1; //分组.
                    let srcOfEntitis = this.mapExt.Tag2; //实体.
                    srcOfGroup = this.DealSQL(srcOfGroup);
                    srcOfEntitis = this.DealSQL(srcOfEntitis);
                    this.groupData = this.DBAccess.RunDBSrc(srcOfGroup, this.mapExt.DBType); //分组.
                    this.entityData = this.DBAccess.RunDBSrc(srcOfEntitis, this.mapExt.DBType); //数据源.
                }
                if(this.entityData.length!=0){
                    let en = this.entityData[0];
                    let idx=0
                    for (let key in en) {
                        if (idx == 0) {
                            this.keyOfGroup = key;
                            idx++;
                            continue;
                        }

                        if (idx == 2) {
                            this.keyOfEn = key;
                            break;
                        }
                        idx++;
                    }

                }

            },
            handleCheckAllChange(val) {
                this.multipleSelect = val ? this.entityData : [];
                this.isIndeterminate = false;
                console.log(this.multipleSelect);
                this.$emit("on-selectRow-change",this.entityData);
            },
            handleCheckedChange(value) {
                let checkedCount = value.length;
                this.checkAll = checkedCount === this.entityData.length;
                this.isIndeterminate =
                    checkedCount > 0 && checkedCount < this.entityData.length;
                let array=[]
                value.forEach(item=>{
                    let newArr = this.entityData.filter(function (obj) {
                        return obj.No == item;
                    });
                    array.push.apply(array,newArr)
                });
                this.$emit("on-selectRow-change",array);
            },
            radioClick(val) {
                //this.multipleSelect = [];
                //this.multipleSelect.push(val);
                //console.log(this.multipleSelect);
                let arry = [val];
                this.$emit("on-selectRow-change",arry);
            },
            DealSQL(dbSrc) {
                //替换表达式常用的用户信息
                dbSrc = dbSrc.replace('@WebUser.No', this.webUser.No);
                dbSrc = dbSrc.replace('@WebUser.Name', this.webUser.Name);
                dbSrc = dbSrc.replace("@WebUser.FK_DeptNameOfFull", this.webUser.FK_DeptNameOfFull);
                dbSrc = dbSrc.replace('@WebUser.FK_DeptName', this.webUser.FK_DeptName);
                dbSrc = dbSrc.replace('@WebUser.FK_Dept', this.webUser.FK_Dept);
                //替换主表中的字段暂时不做处理
                dbSrc = dbSrc.replace(/~/g, "'");
                return dbSrc;

            }
        },

    }
</script>

<style scoped>
    /deep/ .el-checkbox,.el-radio{
        margin-right:10px;
    }
    /deep/ .el-card__body{
        height:400px;
    }
</style>